package com.dlu.safe_construction.service.impl;

import com.dlu.safe_construction.common.ApiResult;
import com.dlu.safe_construction.common.CommonEnum;
import com.dlu.safe_construction.dto.CarInfoDTO;
import com.dlu.safe_construction.mapper.CarMapper;
import com.dlu.safe_construction.mapper.OwnerMapper;
import com.dlu.safe_construction.model.Car;
import com.dlu.safe_construction.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by  lijiaxing on 2022/4/3 16:51
 *
 * @author lijiaxing
 */
@Service
@Slf4j
public class CarServiceImpl implements CarService {

    @Resource
    private CarMapper carMapper;

    @Resource
    private OwnerMapper ownerMapper;


    @Override
    public ApiResult<Void> add(Car car) {
        int ret = carMapper.insert(car);
        return ApiResult.success();
    }

    @Override
    public ApiResult<Void> deleteBy(String key, Integer byWhat) {
        if (CommonEnum.CarDeleteBy.DELETE_BY_CAR_ID.isThisStatus(byWhat)) {
            int ret = carMapper.deleteByPrimaryKey(key);
            return ApiResult.success();
        } else if (CommonEnum.CarDeleteBy.DELETE_BY_OWNER_ID.isThisStatus(byWhat)) {
            int ret = carMapper.deleteByOwnerId(key);
            return ApiResult.success();
        }
        return ApiResult.fail(8000);
    }

    @Override
    public ApiResult<Void> edit(Car car) {
        int ret = carMapper.updateByPrimaryKeySelective(car);
        return ApiResult.success();
    }

    @Override
    public ApiResult<List<CarInfoDTO>> listAll() {
        List<Car> cars = carMapper.selectAll();
        List<CarInfoDTO> result = fromCarList2CarInfoList(cars);
        return ApiResult.success(result);
    }

    private List<CarInfoDTO> fromCarList2CarInfoList(List<Car> cars) {
        List<CarInfoDTO> carInfoDTOs = new ArrayList<>();
        cars.stream().forEach(car -> {
            CarInfoDTO carInfoDTO = new CarInfoDTO();
            carInfoDTO.setCarId(car.getCarId());
            carInfoDTO.setCarStatus(car.getCarStatus());
            carInfoDTO.setOwnerId(car.getOwnerId());
            carInfoDTO.setOwnerName(ownerMapper.selectByPrimaryKey(car.getOwnerId()).getOwnerName());
            carInfoDTOs.add(carInfoDTO);
        });
        return carInfoDTOs;
    }

    @Override
    public ApiResult<List<CarInfoDTO>> searchBy(String key, Integer byWhat) {
        List<Car> cars = null;
        if (CommonEnum.CarSearchBy.SEARCH_BY_CAR_ID.isThisStatus(byWhat)) {
            cars = carMapper.selectByCarId("%" + key + "%");
        } else if (CommonEnum.CarSearchBy.SEARCH_BY_CAR_STATUS.isThisStatus(byWhat)) {
            cars = carMapper.selectByCarStatus(Integer.valueOf(key));
        } else if (CommonEnum.CarSearchBy.SEARCH_BY_OWNER_ID.isThisStatus(byWhat)) {
            cars = carMapper.selectByOwnerId("%" + key + "%");
        }

        return ApiResult.success(fromCarList2CarInfoList(cars));
    }
}
